package com.ruoyi.web.controller.leftover;


import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.LeftoverInDetail;
import com.ruoyi.common.core.domain.entity.LeftoverInMessage;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ILeftoverInService;
import com.ruoyi.system.service.ILeftoverService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 边角料控制层
 *
 * @author C3007106
 */
@Controller
@RequestMapping("/system/leftoverIn")
public class LeftoverInController extends BaseController {

    private String prefix = "leftover/in";

    @Autowired
    private ILeftoverInService leftoverInService;

    @Autowired
    private ILeftoverService leftoverService;

    @GetMapping("/view")
    public String view() {
        return prefix + "/view";
    }

    @GetMapping("/confirm")
    public String confirm() {
        return prefix + "/confirm/view";
    }

    /**
     * 入庫審核
     */
    @RequiresPermissions("system:leftoverIn:confirm")
    @Log(title = "入庫審核", businessType = BusinessType.UPDATE)
    @PostMapping("/confirm")
    @ResponseBody
    public AjaxResult confirm(@Validated LeftoverInDetail leftoverInDetail) {
        leftoverInService.updateLeftoverNum(leftoverInDetail);
        return toAjax(leftoverInService.updateLeftoverInState(leftoverInDetail));
    }

    @RequiresPermissions("system:leftoverIn:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LeftoverInDetail leftoverInDetail) {
        startPage();
        List<LeftoverInDetail> list = leftoverInService.selectLeftoverInList(leftoverInDetail);
        return getDataTable(list);
    }


    /**
     * 修改
     */
    @RequiresPermissions("system:leftoverIn:edit")
    @GetMapping("/edit/{inId}")
    public String edit(@PathVariable("inId") String inId, ModelMap mmap) {
        mmap.put("leftoverIn", leftoverInService.selectLeftoverByTableId(inId));
        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    @RequiresPermissions("system:leftoverIn:edit")
    @Log(title = "邊角料信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated LeftoverInDetail leftoverInDetail) {
        leftoverInService.updateLeftoverInDetail(leftoverInDetail);
        return toAjax(leftoverInService.updateLeftoverIn(leftoverInDetail));
    }

    /**
     * 新增入庫
     */
    @RequiresPermissions("system:leftoverIn:add")
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("leftovers",leftoverService.selectLeftoverIdList(null));
        return prefix + "/add";
    }


    /**
     * 新增保存入庫
     */
    @RequiresPermissions("system:leftoverIn:add")
    @Log(title = "入庫管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated LeftoverInMessage leftoverInMessage) {
        String manage = leftoverInMessage.getManage();
        String receive = leftoverInMessage.getReceive();
        String floor = leftoverInMessage.getFloor();
        LeftoverInDetail[] leftoverInDetails = JSON.parseObject(leftoverInMessage.getLeftoverIn(), LeftoverInDetail[].class);
        return toAjax(leftoverInService.insertLeftoverIn(leftoverInDetails,manage,receive,floor));
    }
}
